import React, { PureComponent } from 'react';
import { connect } from 'dva';
import moment from 'moment';

import { Form, Button, InputNumber, Modal, Input, Select, AutoComplete } from 'antd';

const { Item: FormItem } = Form;
const { Option: AutoCompleteOption } = AutoComplete;
const { Option } = Select;

const getNewBillNumber = () => {
  return 'ZYTC' + moment().format('YYMM');
};

const ModelName = 'mbl';
@connect(({ loading, user }) => ({
  loading: loading.models[ModelName],
  user: user,
}))
@Form.create()
class Comp extends PureComponent {

  constructor(props) {
    super(props);

    const { getInstance } = props;
    if (typeof getInstance === 'function') {
      getInstance(this);
    }
  }


  render() {
    const {
      form: { getFieldDecorator },
      user,
    } = this.props;
    const { visible, onOk, onCancel } = this.props;

    const formLayout = {
      labelCol: {
        sm: { span: 24 },
        lg: { span: 5 },
      },
      wrapperCol: {
        sm: { span: 24 },
        lg: { span: 15 },
      },
    };

    return (
      <Modal title="添加" destroyOnClose visible={visible} onOk={onOk} onCancel={onCancel}>
        <Form onSubmit={this.handleSubmit}>
          <FormItem label="业务员" {...formLayout}>
            {getFieldDecorator('userId', {
              rules: [{ required: true, message: '业务员不能为空' }],
            })(
              <Select>
                {user.list.map(item => (
                  <Option key={item.id}>{item.realName}</Option>
                ))}
              </Select>
            )}
          </FormItem>

          <FormItem label="单号" {...formLayout}>
            {getFieldDecorator('objectNumber', {
              initialValue: getNewBillNumber(),
              rules: [{ required: true, message: '单号不能为空' }],
            })(<Input />)}
          </FormItem>
          <FormItem label="SO号" {...formLayout}>
            {getFieldDecorator('so')(<Input />)}
          </FormItem>

          <FormItem label="年" {...formLayout}>
            {getFieldDecorator('year', {
              initialValue: moment().year(),
            })(<InputNumber />)}
          </FormItem>
          <FormItem label="月份" {...formLayout}>
            {getFieldDecorator('month', {
              initialValue: moment().month() + 1,
            })(<InputNumber />)}
          </FormItem>
        </Form>
      </Modal >
    );
  }
}

export default Comp;
